#!/usr/bin/env bash

is_enabled () {
    echo "$1" | grep -q -i -E "^(yes|on|true|1)$"
}

is_disabled () {
    echo "$1" | grep -q -i -E "^(no|off|false|0)$"
}

get_hrefs () {
    local url="$1"
    local regexp="$2"
    local download_cmd

    if command -v wget >/dev/null 2>&1; then
        download_cmd="wget -q -O-"
    elif command -v curl >/dev/null 2>&1; then
        download_cmd="curl -s -o-"
    else
        echo "ERROR: Neither wget or curl is available, unable to perform download"
        exit 1
    fi

    $download_cmd "${url}" | sed -E "s/></>\n</g" | sed -n -E "s|^.*<a href=\"(${regexp})\">.*|\1|p" | uniq
}

get_os_codename () {
    local branch="$1"
    local url="https://dl.winehq.org/wine-builds/ubuntu/dists/"

    # Get the latest two Ubuntu version codenames that Wine is available on
    local os_codenames=()
    while IFS='' read -r line; do os_codenames+=("$line"); done < <(get_hrefs "${url}" "[^:]+/" | sed -E "s|/$||" | grep -v -E "^([p-z]|/)" | sort -r | head -2)

    # Get the latest version of Wine available for each OS codename
    local codename
    local index=0
    local wine_versions=()
    for codename in "${os_codenames[@]}"; do
        local version
        version=$(get_hrefs "${url}${codename}/main/binary-amd64/" "wine-${branch}_.*\.deb" | sed -n -E "s/^wine-${branch}_([0-9]+(\.[0-9]+)*).*$/\1/p" | sort -rV | head -1)
        wine_versions[${index}]="${version}"
        index+=1
    done

    # Determine which OS codename has the latest version of Wine or use the _older_ OS if both the same
    # as many issues when using latest OS. Refer to https://github.com/scottyhardy/docker-wine/issues/92
    local latest_wine_ver
    latest_wine_ver=$(printf '%s\n' "${wine_versions[@]}" | sort -rV | head -1)

    local retval
    if [ "${wine_versions[1]}" == "${latest_wine_ver}" ]; then
        retval=${os_codenames[1]}     # previous Ubuntu version
    else
        retval=${os_codenames[0]}     # latest Ubuntu version
    fi

    # Return the OS codename to use
    echo "${retval}"
}


# Array of command line args to be passed to the build command
BUILD_ARGS=("$@")

# Default values
BUILD_CMD="docker build"
DOCKER_REPO="${DOCKER_REPO:-docker-wine}"
NO_RDP="${NO_RDP:-no}"
WINE_BRANCH="${WINE_BRANCH:-stable}"

# Get the codename for latest version of wine-${WINE-BRANCH}
#UBUNTU_CODENAME="$(get_os_codename "${WINE_BRANCH}")"

# Just use latest if codename unable to be determined
if [ -n "${UBUNTU_CODENAME}" ]; then
    echo "Found latest version of wine-${WINE_BRANCH} is available on Ubuntu ${UBUNTU_CODENAME}"
else
    echo "WARNING: Unable to determine version of Ubuntu to use with Wine, so using latest"
    UBUNTU_CODENAME="latest"
fi

# Use standard Ubuntu image if using NO_RDP
if is_enabled "${NO_RDP}"; then
    BASE_IMAGE="ubuntu"
    TAG="${UBUNTU_CODENAME}"
elif is_disabled "${NO_RDP}"; then
    BASE_IMAGE="scottyhardy/docker-remote-desktop"
    TAG="latest"
else
    echo "ERROR: Invalid value '${NO_RDP}' used for NO_RDP"
    exit 1
fi

if ! docker system info >/dev/null 2>&1; then
    if buildah -v >/dev/null 2>&1; then
        BUILD_CMD="buildah bud"
    else
        echo "ERROR: Docker is not running or not installed, unable to proceed"
        exit 1
    fi
fi

${BUILD_CMD} "${BUILD_ARGS[@]}" \
    --build-arg="BASE_IMAGE=${BASE_IMAGE}" \
    --build-arg="TAG=${TAG}" \
    --build-arg="WINE_BRANCH=${WINE_BRANCH}" \
    --pull \
    -t "${DOCKER_REPO}" .
